package cc.yugu.yhdw.webserver.web.controller.security.basic;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.basic.AdImage;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.common.utils.IdUtils;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.config.AppConfig;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.constant.AppConstant;
import cc.yugu.yhdw.webserver.entity.SessionUser;
import cc.yugu.yhdw.webserver.service.basic.AdImageService;
import cc.yugu.yhdw.webserver.service.basic.UserService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
 * 广告图片控制类
 *
 * @Date:Created in 2018/7/16 10:59
 * @Author:
 * @Modified By: zjn 2018/7/16 10:59
 */
@Controller
@RequestMapping(value = "/security/basic/ad_image")
public class AdImageController extends SecurityController {
    @Autowired
    AppConfig config;
    @Autowired
    UserService userService;
    @Autowired
    AdImageService adImageService;

    @RequestMapping(value = "index.htm")
    public void index(Model model, HttpSession session) {
//        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_01_03_11.getValue());
    }
    /**
    * 返回分页查询
    * 
    * @Param: [search, session]
    * @Return: cc.yugu.yhdw.common.entity.json.PageResult
    * @Author By: zjn 2018/7/16  11:00
    */ 
    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(AdImage search, HttpSession session) {
        return PageResult.successResult(adImageService.findPage(search));
    }
    
    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "add.htm")
    public void add() {
    }
    /**
    * 查询返回图片地址和编号
    * 
    * @Param: [model, id]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/16  11:01
    */ 
    @RequestMapping(value = "edit.htm")
    public String edit(Model model, int id) {
        AdImage entity = adImageService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        return "/security/basic/ad_image/edit";
    }
    /**
    * 查询
    * 
    * @Param: [model, id]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/16  11:01
    */ 
    @RequestMapping(value = "view.htm")
    public String view(Model model, int id) {
        AdImage entity = adImageService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity",entity);
        }

        return "/security/basic/ad_image//view";
    }

    @RequestMapping(value = "portrait.htm", method = RequestMethod.GET)
    public void portrait() {
    }
    /**
    * 返回文件路径、文件名
    * 
    * @Param: [file, model]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/16  11:16
    */ 
    @RequestMapping(value = "portrait.htm", method = RequestMethod.POST)
    public String portrait(@RequestParam("file") MultipartFile file, Model model) throws IOException {
        File sourceFile = new File(getAppConfig().tempDir, IdUtils.uuid() + "." + YhdwUtils.getFileSuffix(file.getOriginalFilename()));
        YhdwUtils.makeParentDir(sourceFile);
        file.transferTo(sourceFile);

        model.addAttribute("success", true);
        model.addAttribute("filePath", AppConstant.PATH_TEMP + sourceFile.getName());
        model.addAttribute("fileName", file.getOriginalFilename());
        return "/security/basic/ad_image//portrait_response";
    }
    /**
    * 插入
    * 
    * @Param: [adImage, session]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/16  11:18
    */ 
    @RequestMapping("create.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult create(AdImage adImage , HttpSession session) throws IOException {

        Map<String, File> fileMap = new HashMap<String, File>();
        if(StringUtils.isNotEmpty(adImage.getImagePath()) && adImage.getImagePath().startsWith(AppConstant.PATH_TEMP)) {
            fileMap.put("file", new File(config.appDir, adImage.getImagePath()));
        }else {
            return ExtResult.failResult("请上传图片");
        }
        SessionUser sessionUser = getSessionUser(session);
        adImageService.create(adImage, fileMap);

        return ExtResult.successResult();
    }

    /**
    * 更新
    * 
    * @Param: [adImage]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/16  11:18
    */ 
    @RequestMapping("update.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult update(AdImage adImage) throws IOException {
        Map<String, File> fileMap = new HashMap<String, File>();
        if(StringUtils.isNotEmpty(adImage.getImagePath()) && adImage.getImagePath().startsWith(AppConstant.PATH_TEMP)) {
            fileMap.put("file", new File(config.appDir, adImage.getImagePath()));
        }
        adImageService.update(adImage, fileMap);
        return ExtResult.successResult();
    }
    /**
    * 更新订单编号
    * 
    * @Param: [adImage]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/16  11:19
    */ 
    @RequestMapping("update_order_num.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult updateOrderNum(AdImage adImage) throws IOException {


        adImageService.updateOrderNum(adImage.getId(), adImage.getOrderNum());
        return ExtResult.successResult();
    }
    @RequestMapping("delete.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult delete(int id) {
        return adImageService.delete(id);
    }

}
